package com.sinven.service;

import com.sinven.common.ActionResult;
import com.sinven.mapper.ApplyMapper;
import com.sinven.pojo.Apply;
import com.sinven.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApplyServieImpl implements ApplyService {
    @Autowired
    private ApplyMapper applyMapper;
    @Override
    public ActionResult<List<Apply>> queryCurPageData(Integer curpage, Integer pageSize,Integer status) {
        //得到总记录数
        long totalRecords = applyMapper.countApplys(status);
        if (totalRecords == 0) {
            return ResultUtil.success(0,0,null);
        }
        if(pageSize == null || pageSize <0 ){
            pageSize = 5;
        }
        //给curPage赋值
        if(curpage == null || curpage<0){
            curpage = 1;
        }
        //计算总页数
        long totalPage = totalRecords % pageSize == 0?
                totalRecords / pageSize :
                totalRecords / pageSize +1;
        //防止页面恶意传值 超出部分直接显示为最后一页
        if(curpage > totalPage){
            curpage = (int)totalPage;
        }
        long startIndex = (curpage - 1) * pageSize;
        List<Apply> applies = applyMapper.queryApplys(startIndex, pageSize,status);
        return ResultUtil.success(0,totalRecords,applies);
    }

    @Override
    public boolean addApply(Apply apply) {
        return applyMapper.addApply(apply) > 0;
    }

    @Override
    public boolean updateApply(Apply apply) {
        return applyMapper.updateApply(apply) > 0 ;
    }

    @Override
    public boolean deleteApply(long id) {
        return applyMapper.deleteApply(id) > 0;
    }
}
